<?php
namespace Home\Controller;

class NewsController extends HomeController
{
    public function _initialize()
    {
        parent::_initialize();
        C('DEFAULT_THEME', 'v4');
    }

    public function lists($type = 0)
    {
        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');
        $nEvent = event('news');
        $map['type'] = intval($type);
        $list = $nEvent->lists($map, $p, $limit);
        if (IS_AJAX) {
            $this->ajaxReturn(['state' => 0, 'info' => '操作成功', 'data' => $list]);
        }

        $this->videos = $this->_video();

        $this->list = $list;
        $this->type = $type;
        $this->title = $type == 0 ? '新闻' : '公告';

        $this->pager = $limit;
        $this->news_count = $nEvent->count($map);

        $this->display();
    }

    public function detail($id = 0)
    {
        $id = intval($id);
        if (!$id) {
            $this->redirect('/');
        }

        $this->news = $news = event('news')->detail($id, false);
        if (empty($news)) {
            $this->redirect('/');
        }
        event('news')->setReadCount($id);
        $this->title = $news['title'];
        if (IS_MOBILE) {
            return $this->display('mobile');
        }
        $this->videos = $this->_video($news['game_id']);
        $this->display();
    }

    private function _video($game_id = 0)
    {
        $topicId = 3;// 编辑选推
        $game_ids = $game_id ? [$game_id] : [];
        $videos = event('Video')->getByTopicAndGame($topicId, $game_ids, 0, 2);

        $filterMap = array('id','title','thumb','pre_uri', 'user','statis');
        $videos = $this->lists_format($videos, $filterMap);
        return $videos;
    }

}